<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          :default-openeds="['1', '5']"
          background-color="#2e303a"
          text-color="#fff"
          active-text-color="#64ae79"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="$route.path"
        >
          <div class="toggle-button" @click="toggleCollapse">
            |||
          </div>
          <el-submenu
            :index="'/' + item.path"
            v-for="item in MenuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + item1.path"
              v-for="item1 in item.children"
              :key="item1.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item1.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      MenuList: [],
      iconObj: {
        125: 'iconfont icon-yonghu',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-order_icon',
        145: 'iconfont icon-shujutongji'
      },
      // 左侧菜单是否折叠
      isCollapse: false
    }
  },
  //  生命周期函数
  created () {
    this.getMenuList()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.replace('/login')
    },
    // 获取主页左侧菜单数据
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status === 200) {
        this.MenuList = res.data
      } else {
        return res.meta.msg
      }
    },
    // 左侧菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    background-color: #393d3f;
    color: #fff;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    div {
      display: flex;
      align-items: center;
      img {
        padding-right: 20px;
      }
      span {
        font-size: 20px;
      }
    }
  }
  .el-container {
    .el-aside {
      background-color: #343742;
      .el-menu {
        border: 0;
        .toggle-button {
          background-color: #4c5160;
          font-size: 10px;
          line-height: 24px;
          color: #fff;
          text-align: center;
          letter-spacing: 0.2em;
          cursor: pointer;
        }
        .el-submenu {
          .iconfont {
            margin-right: 10px;
          }
        }
      }
    }
    .el-main {
      background-color: #eaedf0;
      color: #333;
    }
  }
}
</style>
